package cn.bdqn.pn.utils;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import org.springframework.util.StringUtils;

@Configuration
public class TimeUtil implements Converter<String,Date>{
    public static final SimpleDateFormat DATE = new SimpleDateFormat("yyyy-MM-dd");
    public static final SimpleDateFormat DATE_TIME = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Override
    public Date convert(String input) {
        if (StringUtils.isEmpty(input)) {
            return null;
        }
        Date date = null;
        //根据字符串长度尝试进行日期转换
        if (10 == input.length()) {
            try {
                date = DATE.parse(input);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            return date;
        } else {
            try {
                date = DATE_TIME.parse(input);

            } catch (ParseException e) {
                e.printStackTrace();
            }
            return date;
        }
    }

    //返回当天日期
    public static String dangqian(){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        return df.format(new Date());
    }

    //返回7天前日期
    public static String zhiqian(){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(new Date());
        calendar.add(calendar.DATE,-6);
        return df.format(calendar.getTime());
    }


}
